#include "pcf8576.h"
//#include "i2c.h"
#include "stm32f1xx_hal.h"
//#include "main.h"

extern void pcf8576_drawByte(I2C_HandleTypeDef* i2cn, PCF8576_INIT init, uint8 * buffer, uint8 index, uint8 value) {
	//unsigned char i;
	//unsigned char index;

	/*start_i2c();
	sendbyte(init.slaveAddr);
	sendbyte(init.device_select);
	//sendbyte(init.load_data_pointer);
	sendbyte(index*2 + 0x80);
	sendbyte(init.mode_set);
	
	buffer[index] = value;
	sendbyte(value);
	stop_i2c();*/
	
	
	uint8 buff[10];
	buff[0] = init.device_select;
	buff[1] = init.device_select;
	buff[2] = index*2 + 0x80;
	buff[3] = init.mode_set;
	buff[4] = value;
	
	buffer[index] = value;
	
	HAL_I2C_Master_Transmit(i2cn, init.slaveAddr, buff, 5, 10);
	
}

void pcf8576_drawPixel(I2C_HandleTypeDef* i2cn, PCF8576_INIT init, uint8 * buffer, uint8 seg, uint8 com, uint8 state) {
	//unsigned char i;
	unsigned char index = seg/2;
	unsigned char value = buffer[index];
	
	// 1/4 multi
	// byte index in buff = seg/2;
	// high 4bit or low 4bit seg%2;
	// in high 4bit? 0x80>>com;   in low 4bit, 0x08>>com;
	if(state) {
		if(seg%2) {
			value |= (0x08>>com);
		} else {
			value |= (0x80>>com);
		}
	} else {
		if(seg%2) {
			value &= ~(0x08>>com);
		} else {
			value &= ~(0x80>>com);
		}
	}
	
	buffer[index] = value;
	
	pcf8576_drawByte(i2cn, init, buffer, index, value);
}

